﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Linq.Expressions;

namespace Articles.LinqDynamicWhere
{
    public class Program
    {
        static void Main(string[] args)
        {
            string folder = "";
            //set folder to samples [files] folder in application directory
            if (args.Length < 1)
                folder = Directory.GetCurrentDirectory() + @"\files\";
            //else use the one you provide in argument 1
            else
                folder = args[0];

            Console.WriteLine("Répertoire analysé: \n" + folder);
            Console.WriteLine("==========================================================================");
            Console.WriteLine("");

            Console.Write("1.Tous les fichiers: \n");
            Console.WriteLine("==========================================================================");
            Console.WriteLine(Samples.GetStringOfAllFiles(folder,SearchOption.AllDirectories));
            Console.WriteLine("");

            
            Console.Write("2.Recherche classique dans les fichiers : un pattern *user*.txt \n");
            Console.WriteLine("==========================================================================");

            foreach (FileInfo file in Samples.ClassicSearchInFiles(folder, "user", ".txt", SearchOption.AllDirectories))
            {
                Console.WriteLine(file.Name);
            }
            Console.WriteLine("");


            Console.WriteLine("3.Static Where: tous les fichiers *.xls contenant user");
            Console.WriteLine("==========================================================================");
            
            List<FileInfo> files = Samples.GetFilesByTypeAndKeyword(folder, ".xls", "user");

            foreach (FileInfo file in files)
            {
                Console.WriteLine(file.FullName.Substring(folder.Length));
            }
            Console.WriteLine("");


            Console.WriteLine("4.Dynamic Where: tous les fichiers *.txt ou *.doc contenant 2 ou 3");
            Console.WriteLine("==========================================================================");
            
            string[] extensions = new string[] { ".txt", ".doc" };
            string[] names = new string[] { "3", "2" };

            DirectoryInfo d = FileSearch.Folder(folder);
            
            d.SearchInFiles(extensions, names, SearchOption.AllDirectories)
                .ForEach(
                    f => Console.WriteLine(f.FullName.Substring(folder.Length))
                    );
           
            Console.WriteLine("");

            Console.WriteLine("5. Just for fun another Dynamic Where: tous les fichiers *.txt");
            Console.WriteLine("==========================================================================");
            extensions = new string[] { ".txt" };
            names = names.Take<string>(0).ToArray<string>();
                       
            d.SearchInFiles(extensions, names, SearchOption.AllDirectories)
                .ForEach(f => Console.WriteLine(f.FullName.Substring(folder.Length)));
            
            Console.WriteLine("");

            Console.Read();
        }

        


    

        

    }





}
